package indi.zhifa.study2024.common.auth.util;

import indi.zhifa.core.auth.util.ITokenUtil;
import indi.zhifa.core.common.web.entity.exception.ServiceException;
import indi.zhifa.core.dataauth.sqlHelper.ISqlVarProvider;
import indi.zhifa.study2024.common.auth.token.AuthObject;
import lombok.RequiredArgsConstructor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

@RequiredArgsConstructor
@Component
public class SqlVarProvider implements ISqlVarProvider {

    private final ITokenUtil mTokenUtil;

    public Expression getVar(String pKey){

        if(pKey.startsWith("auth.")){
            AuthObject authObject = mTokenUtil.getAuthObject();
            String fieldName = pKey.substring("auth.".length());
            switch(fieldName){
                case "enpId" ->{
                    return new LongValue(authObject.getEnpId());
                }
                case "enpCode" ->{
                    return new StringValue(authObject.getEnpCode());
                }
                case "userId" ->{
                    return new LongValue(authObject.getUserId());
                }
                case "userName" ->{
                    return new StringValue(authObject.getUserName());
                }
            }
        }
        throw new ServiceException("自动鉴权暂时不支持key: "+pKey);
    }
}
